package cn.edu.bzu.group4.stockplus.service;

import java.util.Date;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.bzu.group4.stockplus.dao.HibernateUserDao;
import cn.edu.bzu.group4.stockplus.dao.UserDao;
import cn.edu.bzu.group4.stockplus.po.User;


@Service
public class UserServiceImpl implements UserService{
	
	private HibernateUserDao userdao = null;
	
	@Autowired
	private void setUserDao(HibernateUserDao userdao){
		this.userdao = userdao;
	}
	
	

	@Override
	public User getCurrentUser() {
		// 获取当前用户
		 final Long currentUserId = (Long) SecurityUtils.getSubject().getPrincipal();
	        if( currentUserId != null ) {
	            return getUser(currentUserId);
	        } else {
	            return null;
	        }
	}

	@Override
	public void createUser(String username, String email, String password,
			String tel, String hobby, String sex, short account) {
		// 创建用户
		User user = new User();
		
		user.setUserName(username);
		user.setUserEmail(email);
		user.setUserPassword(password);
		user.setUserHobby(hobby);
		short act = 0;
		user.setUserAccount(act);
		user.setUserSex(sex);
		user.setUserTel(tel);
		String now = Long.toString(new Date().getTime()); 
		user.setRecordDate(now);
		
		userdao.createUser(user);
	}

	@Override
	public List<User> getAllUsers() {
		// 获取所有用户
		return userdao.getAllUsers();
	}

	@Override
	public User getUser(Long userId) {
		// 根据id查用户
		return userdao.getUser(userId);
	}

	@Override
	public void deleteUser(Long userId) {
		// 删除用户
		userdao.deleteUser(userId);
	}

	@Override
	public void updateUser(User user) {
		// 更新用户信息
		userdao.updateUser(user);
	}

	@Override
	public User selectUser(String tel, String password) {
		return userdao.selectUser(tel, password);
	}
	
	/*public boolean userLogin(String tel,String password){
		System.out.println("UserService: "+tel+password);
		return userdao.login(tel, password);
	}
	
	public boolean userRegist(User user){
		System.out.println("Test======== User: "+user.getUserAccount());
		return userdao.regist(user);
	}*/
	
	
	
}
